<template>
    <orh-table add="gonwe_add" del @on-add="addShow=true" @on-del="moralDel" pages :pageDate="pageDate"
               @on-page-change="changepage" search @on-search='searchblurry' reload @on-reload="refresh">
        <Select slot="option" v-model="nowxqid" @on-change="xqchange" style="width:200px">
            <Option v-for="item in xqlist" :value="item.semester_id" :key="item.semester_id">{{ item.semester_name.zh?
                item.semester_name.zh: item.semester_name.mn }}
            </Option>
        </Select>
        <Table slot="table" border :columns="columns" :data="datas" @on-selection-change="selectcg"
               ref="currentRowTable" :loading="loading">
        </Table>
        <orh-sidebar slot="others" v-model="addShow" title="添加公文" scale>
            <Operation @close="addShow=false" type="add" @reload="load"></Operation>
        </orh-sidebar>
        <orh-sidebar slot="others" v-model="editShow" title="编辑公文" scale>
            <Operation :id="id" @close="editShow=false" @reload="load" type="edit"></Operation>
        </orh-sidebar>
        <orh-sidebar slot="others" v-model="modelShow" title="公文详情" scale printer @on-printer="printingClick"
                     :add="false" :del="false">
            <document-detail :id="id" :lookdetails='lookdetails' ref="docmanageData"></document-detail>
        </orh-sidebar>

    </orh-table>
</template>

<script>

    export default {
        layout: "menu",
        name: 'documents-docmanage',
        data() {
            return {
                lookdetails: false,
                id: '',
                addShow: false,
                modelShow: false,
                editShow: false,
                gongwenid: [],
                editDate: {},
                pageDate: {},
                loading: false,
                columns: [
                    {
                        type: 'selection',
                        width: 80,
                        align: 'center',
                    },
                    {
                        title: '标题',
                        render: (h, params) => {
                            return h('span', params.row.title.zh ? params.row.title.zh : params.row.title.mn)
                        }
                    },
                    {
                        title: '来文机关',
                        render: (h, params) => {
                            return h('span', params.row.civil_service.zh ? params.row.civil_service.zh : params.row.civil_service.mn)
                        }
                    },
                    {
                        title: '来文字号',
                        render: (h, params) => {
                            return h('span', params.row.letter_number.zh ? params.row.letter_number.zh : params.row.letter_number.mn)
                        }
                    },
                    {
                        title: '收文时间',
                        key: 'receipt_time',
                    },
                    {
                        title: '等级文号',
                        key: 'document_code',
                    },
                    {
                        title: '等级',
                        render: (h, params) => {
                            return h('span', params.row.grade.zh ? params.row.grade.zh : params.row.grade.mn)
                        }
                    },
                    {
                        title: '类型',
                        render: (h, params) => {
                            let dictionary = params.row.dictionary
                            return h('span', dictionary.dictionary_name.zh || dictionary.dictionary_name.mn)
                        }
                    },
                    {
                        title: '审核状态',
                        // width: 180,
                        render(h, params) {
                            let str = ''
                            let color = ''
                            switch (params.row.examine_status) {
                                case 0:
                                    str = '未审核'
                                    color = '#007d7f'
                                    break;
                                case 1:
                                    str = '已通过'
                                    color = '#19be6b'
                                    break;
                                case 2:
                                    str = '未通过'
                                    color = '#ed3f14'
                                    break;
                                case 3:
                                    str = '审核中'
                                    color = '#ed3f14'
                                    break;
                            }
                            return h('span', {style: {color}}, str)
                        }
                    },
                    {
                        title: '时间',
                        key: 'created_at',
                    },
                    {
                        title: '拟办意见',
                        render: (h, params) => {
                            return h('span', params.row.options.zh ? params.row.options.zh : params.row.options.mn)
                        }
                    },
                    {
                        title: '操作',
                        key: 'operation',
                        width: 190,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'success',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
                                            this.modelShow = true
                                            this.lookdetails = true
                                            this.id = params.row.document_id
                                        }
                                    }
                                }, '查看'),
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
                                            this.itemUpdate(params.row)
                                        }
                                    }
                                }, '编辑'),
                                h('Poptip', {
                                    props: {
                                        confirm: true,
                                        title: '您确定要删除这条数据吗?',
                                        transfer: true,
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        'on-ok': () => {
                                            this.delete(params.row.document_id, params.index)
                                        }
                                    }
                                }, [
                                    h('Button', {
                                        style: {
                                            marginRight: '5px',
                                        },
                                        props: {
                                            type: 'error',
                                            size: 'small'
                                        }
                                    }, '删除')
                                ])
                            ]);
                        }
                    }
                ],
                school_name: {},
                typeUrl: 'api/base/v1/school/get/schoolType',
                url: 'api/document/v1/document/my_index',
                delUrl: 'api/document/v1/document/delete/',
                pageUrl: 'api/document/v1/document/my_index?page=',
                datas: [],
                idList: [],
                nowxqid: '',
                xqlist: []
            }
        },
        methods: {
            // ==================================================
            // 打印
            // ==================================================
            printingClick() {
                this.$print(this.$refs.docmanageData)
            },
            load(id = null) {
                let url = 'api/document/v1/document/my_index';
                if (id) url += '?semester_id=' + id
                this.loading = true
                this.$http.get(url).then(val => {
                    const page = {
                        current_page: val.current_page,
                        current_page: val.current_page,
                        first_page_url: val.first_page_url,
                        from: val.from,
                        last_page: val.last_page,
                        last_page_url: val.last_page_url,
                        next_page_url: val.next_page_url,
                        path: val.path,
                        per_page: val.per_page,
                        prev_page_url: val.prev_page_url,
                        to: val.to,
                        total: val.total
                    }
                    this.pageDate = page
                    this.datas = val.data.data
                    this.loading = false
                }).catch(err => {
                    console.log(err)
                    this.loading = false
                })
            },
            //搜索
            searchblurry(val) {
                this.loading = true
                let value = 1
                this.$http.get('api/document/v1/document/my_index?searchvalue=' + val + '&page=' + value).then(val => {
                    if (val.code == 200) {
                        this.pageSize = val.data.per_page
                        this.dataCount = val.data.total
                        this.current = val.data.current_page
                        this.datas = val.data.data
                    }
                    this.loading = false
                }, err => console.log(err))
            },
            //刷新
            refresh() {
                this.changepage(this.pageDate.current_page)
            },
            requery(state) {
                if (state == true) {
                    this.datas(this.current);
                }
            },
            moralDel() {
                let url = this.delUrl + JSON.stringify(this.idList);
                this.$http.delete(url).then(
                    val => {
                        this.load()
                    }
                ).catch(
                    err => console.log(err)
                )
            },
            selectcg(arrs) {
                let ids = arrs.map((item) => {
                    return item.document_id
                })
                this.idList = ids;
                console.log(this.idList)
            },
            // 编辑数据
            itemUpdate(item) {
                this.editShow = true
                this.id = item.document_id
            },
            newList() {
                this.$http.get(this.url).then(val => {
                    val.data.data.forEach((item, index) => {
                        item.school_type = item.school_type.school_type_name
                        item.region = item.region.region_name
                    })
                    this.pageSize = val.data.per_page
                    this.dataCount = val.data.total
                    this.current = val.data.current_page
                    this.datas = val.data.data
                }, err => console.log(err))
            },
            changepage(index) {
                this.loading = true
                this.$http.get(this.pageUrl + index).then(val => {
                    this.pageSize = val.data.per_page
                    this.dataCount = val.data.total
                    this.current = val.data.current_page
                    this.datas = val.data
                    this.loading = false
                }, err => console.log(err))
            },
            // 删除单条数据
            delete(dataid, msg) {
                var jsonid = []
                jsonid.push(dataid)
                this.$http.delete(this.delUrl + JSON.stringify(jsonid)).then(val => {
                    this.datas.splice(msg, 1);
                }, err => console.log(err))
            },
            getnowxq() {
                this.$http.get('api/semester/v1/semester/now_semester').then(res => {
                    this.nowxqid = res.data[1].semester_id
                }).catch(err => {
                    console.log('err', err)
                })
            },
            getxqlist() {
                this.$http.get('api/semester/v1/semester/semesterlists').then(res => {
                    console.log('xqlist', res)
                    this.xqlist = res.data
                }).catch(err => {
                    console.log('err', err)
                })
            },
            xqchange(val) {
                this.load(val)
            }
        },
        components: {
            Operation: re => require(['./Operation.vue'], re),
            DocumentDetail: re => require(['./DocmDtail.vue'], re)
        },
        created() {
            this.load()
            this.getnowxq()
            this.getxqlist()
        }
    }
</script>
